<?php
namespace Admin\Model;
use Think\Model;

class FeeModel extends Model
{
    protected $tableName = "fee";
    protected $tablePrefix = "dangjian_";
    protected $showRow = 10;
    //查看每年的缴纳总金额
    public function queryFee ()
    {
        $res = $this->field("sum(price) total_price,date_format(effect_time,'%Y') year")
            ->group("date_format(effect_time,'%Y')")
            ->order("year desc")
            ->select();
        return $res;
    }

    //查看缴费记录详情,根据年份
    public function queryFeeByYear ($year)
    {
        $count = $this->where("year(effect_time) = $year")->count();
        $page = new \Think\Page($count,$this->showRow);
        $show = $page->show();
        $list = $this->alias("f")
            ->join("dangjian_user u on u.id=f.user_id")
            ->field("f.id,u.name,f.price,f.effect_time,f.create_time")
            ->where("year(f.effect_time) = $year")
            ->order("effect_time")
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        $res['show'] = $show;
        $res['list'] = $list;
        return $res;

    }

    //查询党费缴纳总记录
    public function queryAllFee($data)
    {
        $name = $data['name'];
        $year = $data['year'];
        $map = array();
        if(empty($name)){
        }else{
            $map['u.name'] = array("LIKE", '%' . $name . '%');
        }
        if(empty($year)){
        }else{
            $map['year(f.create_time)'] = $year;
        }

        $order = $data['order'];//排序字段
        $order1 = $data['order1'];//排序方式
        if(empty($order)){
            $order = "f.id";
        }

        if(empty($order1)){
            $order1 = "asc";
        }

        $count = $this->alias("f")
            ->join("dangjian_user u on u.id=f.user_id")
            ->where($map)
            ->count();
        $page = new \Think\Page($count,$this->showRow);
        $show = $page->show();

        $list = $this->alias("f")
            ->join("dangjian_user u on u.id=f.user_id")
            ->field("f.id,u.name,f.price,f.effect_time,f.create_time")
            ->order("$order $order1")
            ->where($map)
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        $res['show'] = $show;
        $res['list'] = $list;
        return $res;
    }

    //查新党费总金额
    public function queryTotalFee ($data)
    {
        $name = $data['name'];
        $year = $data['year'];

        $map = array();
        if(empty($name)){

        }else{
            $map['u.name'] = array("LIKE", '%' . $name . '%');
        }
        if(empty($year)){
//            $map = array();
        }else{
            $map['year(f.create_time)'] = $year;
        }
        $res = $this->alias("f")
            ->join("dangjian_user u on u.id=f.user_id")
            ->where($map)->field("sum(f.price) total")->find();
        return $res;
    }

    public function editFee($data)
    {
        $id =$data['id'];
        unset($data['id']);
        $data['effect_time'] = date("Y-m-d H:i:s",strtotime($data['time']));
        $res = $this->where("id=$id")->save($data);
        return $res;
    }

    public function queryMoneyById($id)
    {
        $res = $this -> where("id=$id")->field("id,price,effect_time,create_time")->select();
        return $res;
    }
}